import BaseService from './_BaseService'
import axios from 'axios'
import config from '../config'
import moment from 'moment'

class ShipLocationService extends BaseService {
  root = '/ais/ship-locations'
  getShipsInRect (southwest, northeast, fromTime = 0) {
    let m = new moment(fromTime)
    let aisTime = m.utc().format('YYYY-MM-DDTHH:mm:ss') + 'Z'
    // console.log('time is::::::::::::::', aisTime)
    let url = `${this.root}?size=999&aisTime.greaterThan=${aisTime}`
    url += `&latitude.greaterThan=${southwest.lat}`
    url += `&latitude.lessThan=${northeast.lat}`
    url += `&longitude.greaterThan=${southwest.lng}`
    url += `&longitude.lessThan=${northeast.lng}`
    return axios.get(config.SERVER_URL + url, {responseType: 'json'})
      .then(resp => resp.data)
  }

  getShipByMmsi (mmsi) {
    return super.get(this.root + '?terminalCode.equals=' + mmsi, {responseType: 'json'})
      .then(resp => resp.data)
  }
}

export default new ShipLocationService()
